<?php


namespace app\service\utils;


use EasyWeChat\Factory;
use think\facade\Log;

class WeiXinOfficialService
{
    protected $app;

    // 接单成功提醒 对用户
    const ACCEPT_ORDER_SUCCESS = 'vjMGh1vy2RMUm0qMg2pbWWcBY1w6Ej1puF8Cr9cCLY8';
    /**
     * 约车取消通知 对教练
     * 学员：{{keyword1.DATA}}
     * 预约时间：{{keyword2.DATA}}
     * 科目：{{keyword3.DATA}}
     */
    const CANCEL_ORDER = 'qULoUW-NatRlGwdPcIcHu8YYQAyg6q29T-BhMeCmXmU';
    // 订单提交成功提醒 对用户
    const ORDER_SUBMIT_SUCCESS = 'nhjm2FzxwzJzOLy09W4MQzCdSLHlwKJvRhWBTulQgDg';
    /**
     * 提现失败提醒
     * 提现金额：{{keyword1.DATA}}
     * 提现时间：{{keyword2.DATA}}
     * 失败原因：{{keyword3.DATA}}
     */
    const WITHDRAW_FAIL = 'kuRKIMxMohfZ8jBbU2f0UZu2ICukXPJCTLriJWJPXYc';
    // 订单取消提醒 对用户
    const ORDER_CANCEL = 'X1xRvtHo2UpQOH3Rv27x-pyWhp3WTwnWrTewnRmOT_Q';
    // 商户拒单通知
    const REFUSE_ACCEPT_ORDER = 'K4NK8RxLa4nNTnmf8UxCjVol7zW9WdDg3FqV8v8bxr4';
    /**
     * 新订单通知
     * 订单编号：{{keyword1.DATA}}
     * 服务类型：{{keyword2.DATA}}
     * 客户姓名：{{keyword3.DATA}}
     * 客户电话：{{keyword4.DATA}}
     */
    const NEW_ORDER = 'K1lr-j9i8WXNV2xTrmlgEhYsmqcuC7Hj5eXd4j7RI2E';
    /**
     * 提现申请
     * 提现金额：{{keyword1.DATA}}
     * 提现时间：{{keyword2.DATA}}
     * 账户余额：{{keyword3.DATA}}
     * 到账卡号：{{keyword4.DATA}}
     */
    const WITHDRAW_APPLY = '7F71mYzA-9gxI3ssiOTGogDMlxRf3LoftnaQoc3KR08';
    /**
     * 提现成功通知
     * 提现金额：{{keyword1.DATA}}
     * 审核结果：{{keyword2.DATA}}
     * 到账时间：{{keyword3.DATA}}
     */
    const WITHDRAW_SUCCESS = '6C1FjiNR1lBuGpXeeEbACBMtiC-yhGROBAuJ7h9lb80';

    /**
     * 打款成功通知
     * 打款日期：{{keyword1.DATA}}
     * 打款时间：{{keyword2.DATA}}
     * 打款内容：{{keyword3.DATA}}
     */
    const RECEIVE_MONEY = '6o6B1JaHpB7yOO6v3UrTpZhnpi9RcrRMEdGXiaAM0aA';

    public function __construct()
    {

        $appId = config('payment.official_app_id');
        $secret = config('payment.official_secret');


        $config = [
            'app_id' => $appId,
            'secret' => $secret,

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => public_path() . 'wechat.log',
            ],
        ];

        $this->app = Factory::officialAccount($config);
    }

    /**
     * 微信公众号授权
     * @param $code
     * @return \Overtrue\Socialite\User
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
     */
    public function wxAuth($code)
    {
        $user = $this->app->oauth->userFromCode($code);
        return $user;
    }

    /**
     * 发送模板消息
     * @param $openId
     * @param $templateId
     * @param $url
     * @param $pagePath
     * @param $data
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sendTemplateMsg($openId, $templateId, $url, $pagePath, $data)
    {
        return $this->app->template_message->send([
            'touser' => $openId,
            'template_id' => $templateId,
            'url' => $url,
//            'miniprogram' => [
//                'appid' => config('payment.miniprogram_app_id'),
//                'pagepath' => $pagePath,
//            ],
            'data' => $data,
        ]);
    }
}